package net.solvetheriddle.wwclient.activity;

import java.util.List;

import net.solvetheriddle.wwclient.R;
import net.solvetheriddle.wwclient.database.WWDatabaseManager;
import net.solvetheriddle.wwclient.entity.Project;
import net.solvetheriddle.wwclient.entity.Work;
import net.solvetheriddle.wwclient.view.GraphView;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.TextView;

public class StatisticsGraphActivity extends Activity {

  private static final int DAY_STATISTICS = 0;
  private static final int WEEK_STATISTICS = 1;
  private static final int MONTH_STATISTICS = 2;
  private static final int COMPLETE_STATISTICS = 3;

  private ImageButton leftArrow;
  private ImageButton rightArrow;
  private int currentPage = 0;
  private TextView statisticsName;
  private TextView summary;
  private GraphView graph;

  private WWDatabaseManager db;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.statisticsgraph);
    db = new WWDatabaseManager(getApplicationContext());

    setupViews();
    updateUI();
    addButtonListeners();
    drawGraph();
  }

  protected void onDestroy() {
    super.onDestroy();
  }

  /**
     * 
     */
  private void setupViews() {
    statisticsName = (TextView) findViewById(R.id.timePeriod);
    summary = (TextView) findViewById(R.id.summary);
    graph = (GraphView) findViewById(R.id.graph);
    leftArrow = (ImageButton) findViewById(R.id.leftArrow);
    rightArrow = (ImageButton) findViewById(R.id.rightArrow);
  }

  /**
   * Adds button listeners
   */
  private void addButtonListeners() {
    leftArrow.setOnClickListener(new OnClickListener() {
      public void onClick(View v) {
        currentPage--;
        updateUI();
        drawGraph();
      }
    });

    rightArrow.setOnClickListener(new OnClickListener() {
      public void onClick(View v) {
        currentPage++;
        updateUI();
        drawGraph();
      }
    });
  }

  private void updateUI() {
    // Updating top buttons
    if (currentPage > 0)
      leftArrow.setEnabled(true);
    else
      leftArrow.setEnabled(false);
    if (currentPage < COMPLETE_STATISTICS)
      rightArrow.setEnabled(true);
    else
      rightArrow.setEnabled(false);

    switch (currentPage) {
    case DAY_STATISTICS:
      statisticsName.setText(R.string.statisticsDay);
      summary.setText(R.string.statisticsDay);
      break;
    case WEEK_STATISTICS:
      statisticsName.setText(R.string.statisticsWeek);
      summary.setText(R.string.statisticsWeek);
      break;
    case MONTH_STATISTICS:
      statisticsName.setText(R.string.statisticsMonth);
      summary.setText(R.string.statisticsMonth);
      break;
    case COMPLETE_STATISTICS:
      statisticsName.setText(R.string.statisticsComplete);
      summary.setText(R.string.statisticsComplete);
      break;
    default:
      throw new IllegalArgumentException("currentStatistics");
    }

  }

  private void drawGraph() {

    float[] values = null;
    String[] verlabels = null;
    String[] horlabels = null;
    float maximum = 0;
    int numberOfHorlables = 0;
    db.open();
    List<Project> projects = db.getAllProjects();
    numberOfHorlables = projects.size();
    values = new float[numberOfHorlables];
    horlabels = new String[numberOfHorlables];
    for (int i = 0; i < projects.size(); i++) {
      Project project = projects.get(i);
      horlabels[i] = project.getName();
      if (horlabels[i].length() > 7) {
        horlabels[i] = horlabels[i].substring(0, 7);
      }
      List<Work> works = db.getWorks(project.getId());
      
      project.setWorks(works);
      values[i] = project.countWorkingHours(currentPage);

      float maxValue = values[i];
      if (maximum < maxValue)
        maximum = maxValue;
    }
    
    db.close();

    verlabels = new String[] { String.valueOf(maximum), String.valueOf(3 * maximum / 4), String.valueOf(maximum / 2), String.valueOf(maximum / 4), "0" };

    graph.setValues(values);
    graph.setVerlabels(verlabels);
    graph.setHorlabels(horlabels);
    graph.setType(GraphView.BAR);
    graph.invalidate();

  }

}
